'use strict';
var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    EntityCollection = require('./EntityCollection'),
    ReferenceRelation = require('./ReferenceRelation');

var _baseField = {
    //版本
    SysVersion: {
        type: Number,
        Name: '版本'
    },
    //创建时间
    CreatedOn: {
        type: Date,
        Name: '创建时间',
        default: Date.now
        // get: inspector
    },
    //创建人
    CreatedBy: {
        type:String,
        Name: '创建人',
        trim: true
    },
    //修改时间
    ModifyOn: {
        type: Date,
        Name: '修改时间',
        default: Date.now
    },
    //修改人
    ModifyBy: {
        type:String,
        Name: '修改人',
        trim: true
    }
    // //打印人
    // PrintPerson: {
    //     type: String,
    //     trim: true
    // },
    // //最后一次打印时间
    // LastPrintDateTime: {
    //     type: Date
    // },
    // //累计打印次数
    // TotalPrintQty: {
    //     type: Number
    // }
};

module.exports = function(modelField){
    var referenceEntitys = {};
	for(var p in _baseField){
		modelField[p] = _baseField[p];
	}

	this.Fields = Object.assign({}, modelField);

    var schemaFields = Object.assign({}, this.FocusedRecord);
    for(var filedName in modelField){
        if(modelField[filedName].hasOwnProperty('IsEmpyt') && modelField[filedName].IsEmpyt === false){
            modelField[filedName].required = '请输入' + modelField[filedName].Name;
            delete modelField[filedName].IsEmpyt;
        }
        if(modelField[filedName].hasOwnProperty('IsUnique') && modelField[filedName].IsUnique === true){
            modelField[filedName].unique = modelField[filedName].Name + '已存在';
            delete modelField[filedName].IsUnique;
        }
    }
	this.Schema = new Schema(modelField);

	//index: 索引，例：{Code: 1}
	//option: 参数，例：{"unique": true}
	this.addIndex = function(index, option){
		this.Schema.index(index, option);
	};

	//entityName: 实体名称，非空空
	//name: 中文名，非空
	//tableName: 表名，默认为entityName
	//parentEntityName: 父实体名称
	this.create = function(entityName, name, tableName, parentEntityName){
		if(!name){
	        name = entityName;
	    }
	    if(!tableName){
	        tableName = entityName;
	    }
	    var parentEntity = null;
	    if(parentEntityName){
	    	parentEntity = EntityCollection.getEntity(parentEntityName);
	    	if(!parentEntity){
	    		throw new Error("父实体 " + parentEntityName + " 不存在，请检查该实体是否加载！");
	    	}
	    }

        for(var filedName in this.Fields){
            var fieldType = this.Fields[filedName].type;
            var fieldRef = this.Fields[filedName].ref;
            if(fieldType.constructor == Array){
                if(fieldType[0].type){
                    fieldType = this.Fields[filedName].type[0].type;
                    fieldRef = this.Fields[filedName].type[0].ref;
                }
            }

            if(fieldType == Schema.ObjectId){
                if(!fieldRef){
                    throw new Error(name + "(" + entityName + "): 字段 " + filedName + " 类型是ObjectId，但没有引用类型");
                }
                ReferenceRelation.addReference(fieldRef, entityName, filedName);
            }
        }

	    mongoose.model(entityName, this.Schema, tableName);
		var newEntity = EntityCollection.addEntity(entityName, name, parentEntity);
		newEntity._Fields = this.Fields;
		return newEntity;
	};
};